package com.gsm.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

@Data
@AllArgsConstructor
@Builder
/**
 * 返回实体
 */
public class Result {
    //是否成功
    private Boolean flag;
    //返回码
    private Integer code;
    //返回信息
    private String message;
    //返回数据
    private Object data;

    /**
     * 默认成功
     */
    public Result() {
        this.flag = true;
        this.code = RespCode.SUCCESS.getCode();
        this.message = RespCode.SUCCESS.getMsg();
    }

    /**
     * 手动指定返回码
     * @param respCode
     */
    public Result(RespCode respCode) {
        this.code = respCode.getCode();
        this.message = respCode.getMsg();
        if (this.code == RespCode.SUCCESS.getCode().intValue()) {
            this.flag = true;
        } else {
            this.flag = false;
        }
    }

    //成功
    public static Result success() {
        Result result = Result.builder()
                .flag(true)
                .code(RespCode.SUCCESS.getCode())
                .message(RespCode.SUCCESS.getMsg())
                .build();
        return result;
    }

    public static Result success(Object obj){
        Result result = success();
        result.setData(obj);
        return result;
    }

    //失败默认
    public static Result error() {
        Result result = Result.builder()
                .flag(false)
                .code(RespCode.ERROR.getCode())
                .message(RespCode.ERROR.getMsg())
                .build();
        return result;
    }

    public static Result error(RespCode respCode) {
        Result result = Result.builder()
                .flag(false)
                .code(respCode.getCode())
                .message(respCode.getMsg())
                .build();
        return result;
    }

    public static void main(String[] args) {

    }
}
